package com.tg.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.recyclerview.widget.RecyclerView;

import com.appbase.custom.config.ApiUrl;
import com.appbase.custom.constant.CommonConstants;
import com.tg.app.R;
import com.tg.app.activity.device.DeviceBusinessActivity;
import com.tg.app.activity.device.add.TipDialogActivity;
import com.tg.appcommon.business.IMessageModule;
import com.tg.data.bean.DeviceItem;
import com.tg.data.http.entity.MessageDataBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public abstract class DeviceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected int radius = 0;
    private static final String TAG = DeviceAdapter.class.getSimpleName();
    protected static final int DEVICE_ITEM_TYPE = 0x01;
    protected static final int ADD_ITEM_TYPE = 0x02;

    protected Context mContext;
    protected List<DeviceItem> mList;
    protected HashMap<String, MessageDataBean> mDoorbellMaps;
    protected DeviceAdapter.OnItemClickListener onItemClickListener;
    protected DeviceAdapter.OnNoItemClickListener onNoItemClickListener;
    protected boolean isLocal = false;

    public void setDoorbellMaps(HashMap<String, MessageDataBean> map) {
        mDoorbellMaps = map;
    }

    public void setLocal(boolean local) {
        isLocal = local;
    }

    public DeviceAdapter(Context context, List<DeviceItem> list) {
        this.mContext = context;
        this.mList = list;
        try {
            radius = (int) context.getResources().getDimension(roundRadiusResId());
            float density = context.getResources().getDisplayMetrics().density;
            float radius1 = (radius / density);
            radius = (int) Math.ceil(radius1);
        } catch (Throwable ex) {}
    }

    protected int roundRadiusResId() {
        return R.dimen.global_customize_fixed_device_card_rounded_radius;
    }

    public void setOnItemClickListener(DeviceAdapter.OnItemClickListener onItemClickListener, DeviceAdapter.OnNoItemClickListener onNoItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        this.onNoItemClickListener = onNoItemClickListener;

    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            if (position < mList.size()) {
                return DEVICE_ITEM_TYPE;
            } else {
                return ADD_ITEM_TYPE;
            }
        }
        return ADD_ITEM_TYPE;
    }


    @Override
    public int getItemCount() {
        return mList == null ? 1 : mList.size() + 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    protected void openMore(DeviceItem item) {
        Intent intent = new Intent(mContext, DeviceBusinessActivity.class);
        intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, item);
        ArrayList<DeviceItem> arrayList = new ArrayList<>();
        arrayList.addAll(mList);
        intent.putExtra(IMessageModule.EXT_EVENT_DEVICES, arrayList);
        ((Activity)mContext).startActivityForResult(intent,0);
    }


    protected void openAlert(DeviceItem item) {
        Intent intent = new Intent();
        intent.putExtra(ApiUrl.DEVICEID, item.id);
        intent.putExtra(ApiUrl.DEVICE_AUTH, item.iccid);
        intent.putExtra(TipDialogActivity.DIALOG_TYPE, 1);
        intent.setClass(mContext, TipDialogActivity.class);
        ((Activity) mContext).startActivityForResult(intent, 0);
    }

    public interface OnItemClickListener {
        void onItemClick(int position, int type);

        void onUpdate();
    }

    public interface OnNoItemClickListener {
        void onViewClick();
    }
    public interface OnDoorbellLoadListener {
        void onLoadMore(DeviceItem deviceItem);
    }
}
